package im.xz.cn.xiacore;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import im.xz.cn.xiacore.auth.Encode;
import im.xz.cn.xiacore.auth.authMain;
import im.xz.cn.xiacore.chat.chatMain;
import im.xz.cn.xiacore.commands.normal.*;
import im.xz.cn.xiacore.entities.events.creeper;
import im.xz.cn.xiacore.events.join;
import im.xz.cn.xiacore.commands.gemini.gma;
import im.xz.cn.xiacore.commands.gemini.gmc;
import im.xz.cn.xiacore.commands.gemini.gms;
import im.xz.cn.xiacore.commands.gemini.gmsp;
import im.xz.cn.xiacore.events.lobbyProtect.qwq;
import im.xz.cn.xiacore.events.lobbyProtect.www;
import im.xz.cn.xiacore.menu.mainMenu.menu;
import im.xz.cn.xiacore.menu.mainMenu.menuEvent;
import im.xz.cn.xiacore.menu.mainMenu.menu_gemini;
import im.xz.cn.xiacore.menu.mainMenu.openMenu;
import im.xz.cn.xiacore.menu.playerInfoMenu.playerMenuEvent;
import im.xz.cn.xiacore.menu.shopMenu.shopEvent;
import im.xz.cn.xiacore.pvpModel.awa;
import im.xz.cn.xiacore.pvpModel.qaq;
import im.xz.cn.xiacore.tab.tabMain;
import im.xz.cn.xiacore.tpPlayer.tpp;
import im.xz.cn.xiacore.tpPlayer.tpp_accept;
import im.xz.cn.xiacore.tpPlayer.tpp_logoutEvent;
import im.xz.cn.xiacore.tpPlayer.tpp_throw;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.PluginMessageListener;
public final class XiaCore extends JavaPlugin implements PluginMessageListener {
    public static Economy econ = null;
    private static XiaCore instance;
    public static XiaCore getInstance(){
        return instance;
    }
    private boolean setupEconomy() {
        if (getServer().getPluginManager().getPlugin("Vault") == null) {
            return false;
        }
        RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
        if (rsp == null) {
            return false;
        }
        econ = rsp.getProvider();
        return econ != null;
    }
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if (!channel.equals("BungeeCord")) {
            return;
        }
        ByteArrayDataInput in = ByteStreams.newDataInput(message);
        String subchannel = in.readUTF();
        if (subchannel.equals("SomeSubChannel")) {
        }
    }
    @Override
    public void onEnable() {
        try {
            getConfig().options().copyDefaults();
            saveDefaultConfig();
            if (im.xz.cn.xiacore.apis.papi.p.a()) {
                if (!setupEconomy() ) {
                    getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                    getServer().getPluginManager().disablePlugin(this);
                    return;
                }
                getServer().getPluginManager().registerEvents(new im.xz.cn.xiacore.events.lobbyProtect.awa(),this);
                getServer().getPluginManager().registerEvents(new qwq(),this);
                getServer().getPluginManager().registerEvents(new www(),this);
                getServer().getPluginManager().registerEvents(new tpp_accept(),this);
                getServer().getPluginManager().registerEvents(new tpp_logoutEvent(),this);
                getServer().getPluginManager().registerEvents(new tpp_throw(),this);
                getCommand("tpp").setExecutor(new tpp());
                getCommand("tpplayer").setExecutor(new tpp());
                getCommand("传送玩家").setExecutor(new tpp());
                getCommand("menu").setExecutor(new menu());
                getCommand("cd").setExecutor(new menu());
                getCommand("菜单").setExecutor(new menu());
                getCommand("helpme").setExecutor(new helpPage());
                getCommand("helppage").setExecutor(new helpPage());
                getCommand("pvp").setExecutor(new awa());
                getCommand("战斗开关").setExecutor(new awa());
                getCommand("amicheated").setExecutor(new authMain());
                getServer().getPluginManager().registerEvents(new join(),this);
                getServer().getPluginManager().registerEvents(new openMenu(),this);
                getServer().getPluginManager().registerEvents(new qaq(),this);
                getCommand("验证").setExecutor(new authMain());
                getServer().getPluginManager().registerEvents(new menuEvent(),this);
                getCommand("gmc").setExecutor(new gmc());
                instance = this;
                getCommand("gms").setExecutor(new gms());
                getCommand("gmsp").setExecutor(new gmsp());
                getCommand("gma").setExecutor(new gma());
                getServer().getPluginManager().registerEvents(new menu_gemini(),this);
                getServer().getPluginManager().registerEvents(new creeper(),this);
                getServer().getPluginManager().registerEvents(new playerMenuEvent(),this);
                getCommand("when").setExecutor(new when());
                getCommand("现在啥时候").setExecutor(new when());
                getCommand("gethome").setExecutor(new getHome());
                getCommand("backhome").setExecutor(new backHome());
                getCommand("取得家").setExecutor(new getHome());
                getCommand("回家").setExecutor(new backHome());
                this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
                this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
                getCommand("spawn").setExecutor(new spawn());
                getCommand("出生点").setExecutor(new spawn());
                getServer().getPluginManager().registerEvents(new shopEvent(),this);
                getServer().getPluginManager().registerEvents(new tabMain(),this);
                getServer().getPluginManager().registerEvents(new chatMain(),this);
                System.out.println("xiaCore正在正常运行！");
            }else {
                System.out.println("此服务器无法经过安全检测，插件已关闭");
                Bukkit.getServer().getConsoleSender().sendMessage("stop");
                Bukkit.getServer().getPluginManager().disablePlugin(this);
            }
        }catch (NullPointerException ignore) {

        }
    }
    @Override
    public void onDisable() {
    }
}